package com.jia.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.product.entity.Brand;
import com.jia.product.mapper.BrandMapper;
import com.jia.product.mapper.CategoryBrandRelationMapper;
import com.jia.product.service.BrandService;
import com.jia.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 品牌(Brand)表服务实现类
 *
 * @author makejava
 * @since 2025-07-08 18:15:26
 */
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public Page<Brand> pageQuery(long current, long size, String key) {
        Page<Brand> page = new Page<>(current, size);

        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.like("name", key);
            });
        }

        return this.page(page, queryWrapper);
    }

    @Override
    public void updateByIdRelation(Brand brand) {
        updateById(brand);
        String brandName = brand.getName();
        if (!StringUtils.isEmpty(brandName)) {
            // 关联修改
            categoryBrandRelationService.updateDetailByBrand(brand);
        }
    }


}

